import {
	gbs
} from 'config/';
import { Subject } from 'rxjs/Rx';

const bookSubject = new Subject();
export default {
	name: 'editbook',
	components: {},
	data() {
		return {
			fields: [{
				key: 'title',
				desc: '请输入标题',
				label: '标题'
			}, {
				key: 'content',
				desc: '请输入章节内容',
				type: 'textarea',
				label: '章节内容'
			}, {
				key: 'book_id',
				text: 'book_name',
				type: 'searchselect',
				subject: bookSubject,
				list: [],
				desc: '请选择书籍',
				label: '搜索书籍',
				remoteMethod: function (query) {
					window.vue_$.$$api_common_book({
						data: query,
						fn: data => {
							let list = [];
							if (data && data.length) {
								for (let book of data) {
									list.push({
										value: book.id,
										text: book.get('name')
									})
								}
								this.list = list;
							} else {
								this.list = [];
							}
						}
					});
				}
			}, {
				key: 'chapter_index',
				desc: '请输入章节位置',
				label: '章节位置',
				type: 'inputnumber'
			}, {
				key: 'word_count',
				desc: '请输入字数',
				label: '字数',
				type: 'inputnumber'
			}],
			default_value: {
				title: '',
				content: '',
				book_id: '',
				book_name: '',
				chapter_index: 1,
				word_count: 0,
			},
			rules: {
				title: [{
					required: true, message: '请输入标题', trigger: 'blur'
				}],
				content: [{
					required: true, message: '请输入内容', trigger: 'blur'
				}],
				word_count: [{
					required: true, trigger: 'blur'
				}],
			}
		}
	},
	methods: {
		onSubmit({ data, info }) {
			console.log(data, info);
			//如果没有任何文本text，就检测html中是否包含图片img，iframe,如果都没有，代表真的没有数据。
			let param = data;
			if (this.$route.query.id) {
				this.$$api_chapter_update({
					data: {
						data: param,
						obj: this.parseObj
					},
					fn: () => {
						this.$router.push('/main/chapter/list');
					}
				});
			} else {
				this.$$api_chapter_create({
					data,
					fn: () => {
						this.$router.push('/main/chapter/list');
					}
				});
			}
		}
	},
	created() {
	},
	mounted() {
		setTimeout(() => {
			if (this.$route.query.id) {
				var data = this.$route.query.id;
				this.$$api_chapter_find({
					data,
					fn: data => {
						bookSubject.next([{ value: data.book_id, text: data.book_name }]);

						this.default_value.id = data.id;
						this.default_value.title = data.title;
						this.default_value.content = data.content;
						this.default_value.book_id = data.book_id;
						this.default_value.book_name = data.book_name;
						this.default_value.chapter_index = data.chapter_index;
						this.default_value.word_count = data.word_count;
					}
				});
			}

		})
	},
	watch: {
		default_value: {
			deep: true,
			handler(v) {
				this.default_value = v;
			}
		}
	}
}